import os
import time

import pywinauto
from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))

driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://192.168.56.129:8090/console/login")

driver.find_element(By.XPATH, "//*[@id='input_0']").send_keys("admin")
driver.find_element(By.ID, "passwordInput").send_keys("admin")

driver.find_element(By.XPATH, "//*[@id='app']/div/div[1]/button/span").click()

time.sleep(3)

driver.find_element(By.XPATH, "//*[@id='app']/div/aside/div[2]/div[2]/div[2]/ul/li[12]/div/span[2]").click()
time.sleep(1)

driver.find_element(By.LINK_TEXT, "admin").click()
time.sleep(1)
driver.find_element(By.XPATH, "//*[@id='app']/div/main/header/div/div/div[1]/div[1]/div/div[1]/img").click()

time.sleep(1)

driver.find_element(By.XPATH, "//*[text()='上传']").click()  # 此时直接复制xpath行不通 要使用 //*[text()='上传' 来查找元素

# 选择文件上传的窗口 窗口句柄默认为‘打开’
# 使用pywinauto创建一个操作桌面窗口的对象
app = pywinauto.Desktop()
# 选择文件上传的窗口 窗口句柄默认为‘打开’
dialog = app["打开"]
# 文件上传的时间
BASE_DIR = os.path.dirname(__file__)  # 获取项目根路径

file_path = BASE_DIR + os.sep + "1.png"
dialog["Edit1"].type_keys(file_path)
time.sleep(1)
dialog["Button2"].click()  # 如果是谷歌则是button1  如果是edge则是button2
time.sleep(1)
driver.find_element(By.XPATH, "//*[@class='modal-footer']/div/button[1]").click()

time.sleep(3)

